package com.tarena.lbs.attach.dao.repository.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.attach.dao.mapper.AttachMapper;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Slf4j
@Repository
public class AttachRepositoryImpl implements AttachRepository {
    @Autowired
    private AttachMapper attachMapper;

    @Override
    public void insert(AttachPO attachPO) {
        attachMapper.insert(attachPO);
    }

    @Override
    public int batchUpdateAttachByIdOrFileUUID(List<PicUpdateParam> picParams) {
        int updateCount = 0;
        for (PicUpdateParam param : picParams) {
            AttachPO attachPO = new AttachPO();
            attachPO.setId(param.getId());
            attachPO.setFileUuid(param.getFileUuid());
            attachPO.setBusinessType(param.getBusinessType());
            attachPO.setBusinessId(param.getBusinessId());
            // WHERE id = ? OR file_uuid = ?
            QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", param.getId());
            queryWrapper.or();
            queryWrapper.eq("file_uuid", param.getFileUuid());
            attachMapper.update(attachPO, queryWrapper);
            // 更新计数
            updateCount++;
        }
        log.info("批量更新图片绑定信息,更新了{}条数据",updateCount);
        return updateCount;
    }

    @Override
    public List<AttachPO> selectList(AttachQuery attachQuery) {
        QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_type", attachQuery.getBusinessType());
        queryWrapper.eq("business_id", attachQuery.getBusinessId());
        List<AttachPO> attachPOList = attachMapper.selectList(queryWrapper);
        log.debug("查询图片绑定信息,查询结果:{}",attachPOList);
        return attachPOList;
    }
}
